﻿using System;
using System.Collections;
using Castle.ActiveRecord;
using NHibernate;

namespace ApplicationGenerator.DataAccess
{
    public class HQLManager
    {

        /// <summary>
        /// Executes HQL query 
        /// </summary>
        /// <param name="query"> query</param>
        /// <returns>List of results (string[]) or null if something wnet wrong</returns>
        static public  IList ExecuteHQL(string query )
        {

            ISession session    = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(ActiveRecordBase));
            IList results       = null;
            IQuery hqlQuery     = null;

            if (null == session || false == session.IsOpen)
            {
                return null;
            }

            try
            {
                hqlQuery = session.CreateQuery(query);

                if ( null == hqlQuery )
                {
                    return null;
                }

                results = hqlQuery.List();

            }
            catch( Exception  )
            {
                return null;
            }

            return results;
        }

        /// <summary>
        /// Executes HQL query
        /// </summary>
        /// <param name="query">query</param>
        /// <param name="maxResults">max results </param>
        /// <returns>List of results (string[]) or null if something wnet wrong</returns>
        static public IList ExecuteHQL(string query, int maxResults)
        {

            ISession session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(ActiveRecordBase));
            IList results = null;
            IQuery hqlQuery = null;

            if (null == session || false == session.IsOpen)
            {
                return null;
            }

            try
            {
                hqlQuery = session.CreateQuery(query);

                if (null == hqlQuery)
                {
                    return null;
                }

                hqlQuery.SetMaxResults(maxResults);
                results = hqlQuery.List();

            }
            catch (Exception)
            {
                return null;
            }

            return results;
        }


        /// <summary>
        /// Executes HQL query 
        /// </summary>
        /// <param name="query"> query</param>
        /// <returns>List of results (string[]) or null if something wnet wrong</returns>
        static public HQLResult ExecuteHQLR(string query)
        {

            ISession session = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof(ActiveRecordBase));
            IList results = null;
            IQuery hqlQuery = null;

            if (null == session || false == session.IsOpen)
            {
                return null;
            }

            try
            {
                hqlQuery = session.CreateQuery(query);

                if (null == hqlQuery)
                {
                    return null;
                }

                results = hqlQuery.List();

            }
            catch (Exception)
            {
                return null;
            }

            HQLResult res = new HQLResult(query, results);

            return res;
        }

    }
}
